package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Goods;
import cn.wolfcode.car.business.domain.StockBillItem;
import cn.wolfcode.car.business.mapper.GoodsMapper;
import cn.wolfcode.car.business.mapper.StockBillItemMapper;
import cn.wolfcode.car.business.query.GoodsQuery;
import cn.wolfcode.car.business.service.IGoodsService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.util.uuid.UUID;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private StockBillItemMapper stockBillItemMapper;
    @Override
    public TablePageInfo<Goods> query(GoodsQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Goods>(goodsMapper.selectForList(qo));
    }


    @Override
    public void save(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public Goods get(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Goods goods) {
        goodsMapper.updateByPrimaryKey(goods);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            // 货品数量为0才能删除
            Integer amounts = goodsMapper.queryAmounts(dictId);
            if (amounts == null || amounts == 0) {
                goodsMapper.deleteByPrimaryKey(dictId);
            }
        }
    }

    @Override
    public List<Goods> list() {
        return goodsMapper.selectAll();
    }

    @Override
    public void goodsSave(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long id : dictIds) {
            Goods good = goodsMapper.selectByPrimaryKey(id);
            StockBillItem stockBillItem = new StockBillItem();
            stockBillItem.setGoodsId(good.getId());
            stockBillItem.setAmounts(1);
            stockBillItem.setPrice(good.getPrice());
            stockBillItem.setBillId(0L);
            stockBillItemMapper.insert(stockBillItem);
        }
    }
    /*@Override
    public String updateFile(MultipartFile file) throws IOException {
        String extension = FileUploadUtils.getExtension(file);
        if (!("png".equals(extension) || "jpg".equals(extension) || "jpeg".equals(extension) || "gif".equals(extension))) {
            throw new BusinessException("请提交图片，格式为 png jpg gif");
        }
        String uuid = UUID.randomUUID().toString();
        String path = "/goodsCover/" + uuid + "." + extension;
        InputStream in = file.getInputStream();
        OutputStream out = new FileOutputStream(new File("src/main/resources/static" + path));
        String clzPath = new ClassPathResource("static").getFile().getPath();
        OutputStream clzOut = new FileOutputStream(new File(clzPath + path));
        byte[] b = new byte[5096];
        int len = -1;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
            clzOut.write(b, 0, len);
        }
        return path;
    }*/


    @Override
    public String uploadFileMinio(MultipartFile file) throws Exception {
        // 空文件报错
        if (file == null) {
            throw new BusinessException("请上传合同电子附件！");
        }

        // 非PDF文件报错
        String extension = FileUploadUtils.getExtension(file);
        if (!("png".equals(extension) || "jpg".equals(extension) || "jpeg".equals(extension) || "gif".equals(extension))) {
            throw new BusinessException("请提交图片，格式为 png jpg gif");
        }
        return FileUploadUtils.minIoUpload(file);
    }
}
